20. ContentProvider delete() Method
Okay, the final method to implement is delete. So far you've seen three examples of how to implement these methods, so I have faith that you can implement the delete method yourself.
Overview of the delete() Method
From the https://developer.android.com/reference/android/content/ContentProvider.html#delete(android.net.Uri, java.lang.String, java.lang.String[])" target="_blank">documentation for the ContentProvider delete() method, we see the method has 3 inputs - uri, selection, and selectionArgs. The return value is the number of numbers successfully deleted.
Here’s an end-to-end diagram of how the delete() method works.
The UriMatcher helps determine which one of the two cases is performed - the PETS or the PET_ID case. In the PETS case, the caller wants to delete multiple rows in the pets table according to the selection and selection args. In the PET_ID case, the caller wants to delete a specific pet.
The diagram above shows the return value is an integer representing the number of rows delete.
Sample Use Case #1 - Delete multiple rows
Say the shelter threw an “Adopt-A-Calico-Cat” event, where all the Calico breed cats were adopted. This means we should remove all animals whose breed is Calico from our pets table.
Example inputs to delete() method:
URI: content://com.example.android.pets/pets
Selection: “breed=?”
SelectionArgs: { “Calico” }
Within the delete() method:
SQLite statement: DELETE pets WHERE breed= ‘Calico’
Result:
A successful delete operation would return the number of Calico breed cats we started off with in our pet table. For example, if there initially were 10 Calico breed cats in the shelter, after the delete operation we would get back 10 rows.
Sample Use Case #2 - Delete 1 Pet
For example, Milo the french bulldog (@frenchiebutt on instagram) was so adorable a family came by and adopted him! This means we should remove him from our database of pets to be adopted.
Example inputs to delete() method:
URI: content://com.example.android.pets/pets/5
Selection: “name=?”
SelectionArgs: { “Milo” }
Within the delete() method:
SQLite statement: DELETE pets WHERE _id=5
Result:
A successful delete operation would return 1, for one row being deleted.
Code for the delete() Method
Let’s move onto implementing the code now. Replace the current delete() method in your PetProvider class with the one provided below (code also available in this gist).
In PetProvider.java:
public class PetProvider extends ContentProvider {
…
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Get writeable database
SQLiteDatabase database = mDbHelper.getWritableDatabase();
final int match = sUriMatcher.match(uri);
switch (match) {
case PETS:
// Delete all rows that match the selection and selection args
return database.delete(PetEntry.TABLE_NAME, selection, selectionArgs);
case PET_ID:
// Delete a single row given by the ID in the URI
selection = PetEntry._ID + "=?";
selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
return database.delete(PetEntry.TABLE_NAME, selection, selectionArgs);
default:
throw new IllegalArgumentException("Deletion is not supported for " + uri);
}
Your Task: Implement delete() method
Remove the code for the existing delete() method and replace with the code from this gist to get started. Check that the app still compiles.
Delete() Method
SOLUTION:
- Implement the entire `delete()` method in the `PetProvider` class on your own.
- Allow for deleting 1 pet
- Allow for deleting multiple pets
Solution - ContentProvider delete() Method
Let's discuss a bit about the delete() method you just added into the PetProvider.
Delete looks a lot like the other methods. I start by grabbing a writable version of the database. Next, I match the uri. Then I have two different cases, one for deleting all the pets, and one for deleting a single pet.
If neither of these uris are given, an exception is thrown, just like in the other methods. And with that my four CRUD methods are complete
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Get writeable database
SQLiteDatabase database = mDbHelper.getWritableDatabase();
final int match = sUriMatcher.match(uri);
switch (match) {
case PETS:
// Delete all rows that match the selection and selection args
return database.delete(PetEntry.TABLE_NAME, selection, selectionArgs);
case PET_ID:
// Delete a single row given by the ID in the URI
selection = PetEntry._ID + "=?";
selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
return database.delete(PetEntry.TABLE_NAME, selection, selectionArgs);
default:
throw new IllegalArgumentException("Deletion is not supported for " + uri);
}
}
See the full code diff for this code checkpoint here.